import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { useUserStore } from './user.js'
import { insertCartAPI, selectCartAPI, delCartAPI } from "@/apis/cart.js"
export const useCartStore = defineStore('cart', () => {
    const cartList = ref([]);
    const userStore = useUserStore()
    const isLogin = computed(() => userStore.userInfo.token)
    //获取最新的购物车列表
    const updateNewList = async () => {
        const res = await selectCartAPI()
        cartList.value = res.result
    }
    // 添加购物车
    const addCart = async (goods) => {
        const { count } = goods
        const skuId = goods.skuId.skuId
        if (isLogin.value) {
            // 服务器添加购物车
            await insertCartAPI({ skuId, count })
            const res = await selectCartAPI()
            cartList.value = res.result
        } else {
            // 本地添加购物车
            const item = cartList.value.find((item) => goods.skuId === item.skuId)
            if (item) {
                item.count += goods.count
            } else {
                cartList.value.push(goods)
            }
        }
    }

    // 删除购物车
    const delCart = async (skuId) => {
        if (isLogin.value) {
            await delCartAPI([skuId])
            const res = await selectCartAPI()
            cartList.value = res.result
        } else {
            console.log(skuId);
            //filter方法
            cartList.value = cartList.value.filter(item => item.skuId != skuId)
            //splice方法
            // const idx = cartList.value.find((item) => skuId === item.skuId)
            // cartList.value.splice(idx, 1)
        }
    }
    // 退出登录清空购物车
    const clearCart = () => {
        cartList.value = []
    }

    //单选框选中
    const singleCheck = (skuId, selected) => {
        const item = cartList.value.find((item) => item.skuId == skuId)
        item.selected = selected
    }
    // 购物车商品数量总计
    const allCount = computed(() => {
        return cartList.value.reduce((a, c) => a + c.count, 0)
    })
    // 购物车商品价格总计
    const allPrice = computed(() => {
        return cartList.value.reduce((a, c) => a + c.price * c.count, 0)
    })
    // 全选框的状态
    const isAllCheck = computed(() => {
        return cartList.value.every((item) => item.selected)
    });
    // 全选框的操作
    const changeAllCheck = (selected) => {
        cartList.value.forEach((item) => item.selected = selected)
    }
    const allCountCheck = computed(() => {
        //遍历数组中的单选框选中状态，然后累计加上选中后的数量
        return cartList.value.filter((item) => item.selected == true).reduce((a, c) => a + c.count, 0)
    })
    const allPriceCheck = computed(() => {
        //遍历数组中的单选框选中状态，然后累计加上选中后的数量*价格
        return cartList.value.filter((item) => item.selected == true).reduce((a, c) => a + c.count * c.price, 0)
    })


    return {
        cartList, addCart, delCart, allCount, allPrice,
        singleCheck, isAllCheck, changeAllCheck,
        allCountCheck, allPriceCheck, clearCart, updateNewList
    }
}, {
    persist: true
})
